home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / dist / global-alloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-24  |  32.9 KB  |  1,091 lines

  1. /* Allocate registers for pseudo-registers that span basic blocks.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include "config.h"
  23. #include "rtl.h"
  24. #include "flags.h"
  25. #include "basic-block.h"
  26. #include "hard-reg-set.h"
  27. #include "regs.h"
  28. #include "insn-config.h"
  29.  
  30. /* This pass of the compiler performs global register allocation.
  31.    It assigns hard register numbers to all the pseudo registers
  32.    that were not handled in local_alloc.  Assignments are recorded
  33.    in the vector reg_renumber, not by changing the rtl code.
  34.    (Such changes are made by final).  The entry point is
  35.    the function global_alloc.
  36.  
  37.    After allocation is complete, the reload pass is run as a subroutine
  38.    of this pass, so that when a pseudo reg loses its hard reg due to
  39.    spilling it is possible to make a second attempt to find a hard
  40.    reg for it.  The reload pass is independent in other respects
  41.    and it is run even when stupid register allocation is in use.
  42.  
  43.    1. count the pseudo-registers still needing allocation
  44.    and assign allocation-numbers (allocnos) to them.
  45.    Set up tables reg_allocno and allocno_reg to map 
  46.    reg numbers to allocnos and vice versa.
  47.    max_allocno gets the number of allocnos in use.
  48.  
  49.    2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
  50.    Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
  51.    for conflicts between allocnos and explicit hard register use
  52.    (which includes use of pseudo-registers allocated by local_alloc).
  53.  
  54.    3. for each basic block
  55.     walk forward through the block, recording which
  56.     unallocated registers and which hardware registers are live.
  57.     Build the conflict matrix between the unallocated registers
  58.     and another of unallocated registers versus hardware registers.
  59.     Also record the preferred hardware registers
  60.     for each unallocated one.
  61.  
  62.    4. Sort a table of the allocnos into order of
  63.    desirability of the variables.
  64.  
  65.    5. Allocate the variables in that order; each if possible into
  66.    a preferred register, else into another register.  */
  67.  
  68. /* Number of pseudo-registers still requiring allocation
  69.    (not allocated by local_allocate).  */
  70.  
  71. static int max_allocno;
  72.  
  73. /* Indexed by (pseudo) reg number, gives the allocno, or -1
  74.    for pseudo registers already allocated by local_allocate.  */
  75.  
  76. static int *reg_allocno;
  77.  
  78. /* Indexed by allocno, gives the reg number.  */
  79.  
  80. static int *allocno_reg;
  81.  
  82. /* A vector of the integers from 0 to max_allocno-1,
  83.    sorted in the order of first-to-be-allocated first.  */
  84.  
  85. static int *allocno_order;
  86.  
  87. /* Indexed by an allocno, gives the number of consecutive
  88.    hard registers needed by that pseudo reg.  */
  89.  
  90. static int *allocno_size;
  91.  
  92. /* max_allocno by max_allocno array of bits,
  93.    recording whether two allocno's conflict (can't go in the same
  94.    hardware register).
  95.  
  96.    `conflicts' is not symmetric; a conflict between allocno's i and j
  97.    is recorded either in element i,j or in element j,i.  */
  98.  
  99. static int *conflicts;
  100.  
  101. /* Number of ints require to hold max_allocno bits.
  102.    This is the length of a row in `conflicts'.  */
  103.  
  104. static int allocno_row_words;
  105.  
  106. /* Two macros to test or store 1 in an element of `conflicts'.  */
  107.  
  108. #define CONFLICTP(I, J) \
  109.  (conflicts[(I) * allocno_row_words + (J) / INT_BITS]    \
  110.   & (1 << ((J) % INT_BITS)))
  111.  
  112. #define SET_CONFLICT(I, J) \
  113.  (conflicts[(I) * allocno_row_words + (J) / INT_BITS]    \
  114.   |= (1 << ((J) % INT_BITS)))
  115.  
  116. /* Set of hard regs currently live (during scan of all insns).  */
  117.  
  118. static HARD_REG_SET hard_regs_live;
  119.  
  120. /* Indexed by N, set of hard regs conflicting with allocno N.  */
  121.  
  122. static HARD_REG_SET *hard_reg_conflicts;
  123.  
  124. /* Indexed by N, set of hard regs preferred by allocno N.
  125.    This is used to make allocnos go into regs that are copied to or from them,
  126.    when possible, to reduce register shuffling.  */
  127.  
  128. static HARD_REG_SET *hard_reg_preferences;
  129.  
  130. /* Set of registers that some allocno has a preference for.  */
  131.  
  132. static HARD_REG_SET regs_someone_prefers;
  133.  
  134. /* Set of registers that global-alloc isn't supposed to use.  */
  135.  
  136. static HARD_REG_SET no_global_alloc_regs;
  137.  
  138. /* Test a bit in TABLE, a vector of HARD_REG_SETs,
  139.    for vector element I, and hard register number J.  */
  140.  
  141. #define REGBITP(TABLE, I, J)     TEST_HARD_REG_BIT (TABLE[I], J)
  142.  
  143. /* Set to 1 a bit in a vector of HARD_REG_SETs.  Works like REGBITP.  */
  144.  
  145. #define SET_REGBIT(TABLE, I, J)  SET_HARD_REG_BIT (TABLE[I], J)
  146.  
  147. /* Bit mask for allocnos live at current point in the scan.  */
  148.  
  149. static int *allocnos_live;
  150.  
  151. #define INT_BITS HOST_BITS_PER_INT
  152.  
  153. /* Test, set or clear bit number I in allocnos_live,
  154.    a bit vector indexed by allocno.  */
  155.  
  156. #define ALLOCNO_LIVE_P(I) \
  157.   (allocnos_live[(I) / INT_BITS] & (1 << ((I) % INT_BITS)))
  158.  
  159. #define SET_ALLOCNO_LIVE(I) \
  160.   (allocnos_live[(I) / INT_BITS] |= (1 << ((I) % INT_BITS)))
  161.  
  162. #define CLEAR_ALLOCNO_LIVE(I) \
  163.   (allocnos_live[(I) / INT_BITS] &= ~(1 << ((I) % INT_BITS)))
  164.  
  165. /* Record all regs that are set in any one insn.
  166.    Communication from mark_reg_{store,clobber} and global_conflicts.  */
  167.  
  168. static rtx *regs_set;
  169. static int n_regs_set;
  170.  
  171. static int allocno_compare ();
  172. static void mark_reg_store ();
  173. static void mark_reg_clobber ();
  174. static void mark_reg_live_nc ();
  175. static void mark_reg_death ();
  176. static void dump_conflicts ();
  177. static void find_reg ();
  178. static void global_conflicts ();
  179. static void record_conflicts ();
  180. static void set_preference ();
  181.  
  182. /* Perform allocation of pseudo-registers not allocated by local_alloc.
  183.    FILE is a file to output debugging information on,
  184.    or zero if such output is not desired.  */
  185.  
  186. void
  187. global_alloc (file)
  188.      FILE *file;
  189. {
  190.   register int i;
  191.  
  192.   max_allocno = 0;
  193.  
  194.   CLEAR_HARD_REG_SET (regs_someone_prefers);
  195.  
  196.   /* A machine may have certain hard registers that
  197.      are safe to use only within a basic block.  */
  198.  
  199.   CLEAR_HARD_REG_SET (no_global_alloc_regs);
  200. #ifdef OVERLAPPING_REGNO_P
  201.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  202.     if (OVERLAPPING_REGNO_P (i))
  203.       SET_HARD_REG_BIT (no_global_alloc_regs, i);
  204. #endif
  205.  
  206.   /* Establish mappings from register number to allocation number
  207.      and vice versa.  In the process, count the allocnos.  */
  208.  
  209.   reg_allocno = (int *) alloca (max_regno * sizeof (int));
  210.  
  211.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  212.     reg_allocno[i] = -1;
  213.  
  214.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  215.     /* Note that reg_live_length[i] < 0 indicates a "constant" reg
  216.        that we are supposed to refrain from putting in a hard reg.
  217.        -2 means do make an allocno but don't allocate it.  */
  218.     if (reg_n_refs[i] != 0 && reg_renumber[i] < 0 && reg_live_length[i] != -1)
  219.       {
  220.     reg_allocno[i] = max_allocno++;
  221.     if (reg_live_length[i] == 0)
  222.       abort ();
  223.       }
  224.     else
  225.       reg_allocno[i] = -1;
  226.  
  227.   allocno_reg = (int *) alloca (max_allocno * sizeof (int));
  228.   allocno_size = (int *) alloca (max_allocno * sizeof (int));
  229.   bzero (allocno_size, max_allocno * sizeof (int));
  230.  
  231.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  232.     if (reg_allocno[i] >= 0)
  233.       {
  234.     allocno_reg[reg_allocno[i]] = i;
  235.     allocno_size[reg_allocno[i]] = PSEUDO_REGNO_SIZE (i);
  236.       }
  237.  
  238.   /* Allocate the space for the conflict tables.  */
  239.  
  240.   hard_reg_conflicts = (HARD_REG_SET *)
  241.     alloca (max_allocno * sizeof (HARD_REG_SET));
  242.   bzero (hard_reg_conflicts, max_allocno * sizeof (HARD_REG_SET));
  243.  
  244.   hard_reg_preferences = (HARD_REG_SET *)
  245.     alloca (max_allocno * sizeof (HARD_REG_SET));
  246.   bzero (hard_reg_preferences, max_allocno * sizeof (HARD_REG_SET));
  247.  
  248.   allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
  249.  
  250.   conflicts = (int *)
  251.     alloca (max_allocno * allocno_row_words * sizeof (int));
  252.   bzero (conflicts, max_allocno * allocno_row_words * sizeof (int));
  253.  
  254.   allocnos_live = (int *) alloca (allocno_row_words * sizeof (int));
  255.  
  256.   /* If there is work to be done (at least one reg to allocate),
  257.      perform global conflict analysis and allocate the regs.  */
  258.  
  259.   if (max_allocno > 0)
  260.     {
  261.       /* Scan all the insns and compute the conflicts among allocnos
  262.      and between allocnos and hard regs.  */
  263.  
  264.       global_conflicts ();
  265.  
  266.       /* Determine the order to allocate the remaining pseudo registers.  */
  267.  
  268.       allocno_order = (int *) alloca (max_allocno * sizeof (int));
  269.       for (i = 0; i < max_allocno; i++)
  270.     allocno_order[i] = i;
  271.  
  272.       /* Default the size to 1, since allocno_compare uses it to divide by.  */
  273.  
  274.       for (i = 0; i < max_allocno; i++)
  275.     if (allocno_size[i] == 0)
  276.       allocno_size[i] = 1;
  277.  
  278.       qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
  279.  
  280.       if (file)
  281.     dump_conflicts (file);
  282.  
  283.       /* Try allocating them, one by one, in that order,
  284.      except for parameters marked with reg_live_length[regno] == -2.  */
  285.  
  286.       for (i = 0; i < max_allocno; i++)
  287.     if (reg_live_length[allocno_reg[allocno_order[i]]] >= 0)
  288.       {
  289.         /* If we have more than one register class,
  290.            first try allocating in the class that is cheapest
  291.            for this pseudo-reg.  If that fails, try any reg.  */
  292.         if (N_REG_CLASSES > 1)
  293.           {
  294.         find_reg (allocno_order[i], 0, 0, 0,
  295.               hard_reg_preferences[allocno_order[i]]);
  296.         if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
  297.           continue;
  298.           }
  299.         if (!reg_preferred_or_nothing (allocno_reg[allocno_order[i]]))
  300.           find_reg (allocno_order[i], 0, 1, 0,
  301.             hard_reg_preferences[allocno_order[i]]);
  302.       }
  303.     }
  304.  
  305.   /* Do the reloads now while the allocno data still exist, so that we can
  306.      try to assign new hard regs to any pseudo regs that are spilled.  */
  307.  
  308.   if (n_basic_blocks > 0)
  309.     reload (basic_block_head[0], 1, file);
  310. }
  311.  
  312. /* Sort predicate for ordering the allocnos.
  313.    Returns -1 (1) if *v1 should be allocated before (after) *v2.  */
  314.  
  315. static int
  316. allocno_compare (v1, v2)
  317.      int *v1, *v2;
  318. {
  319.   register int r1 = allocno_reg[*v1];
  320.   register int r2 = allocno_reg[*v2];
  321.   /* Note that the quotient will never be bigger than
  322.      the value of floor_log2 times the maximum number of
  323.      times a register can occur in one insn (surely less than 100).
  324.      Multiplying this by 10000 can't overflow.  */
  325.   register int pri1
  326.     = (((double) (floor_log2 (reg_n_refs[r1]) * reg_n_refs[r1])
  327.     / (reg_live_length[r1] * allocno_size[*v1]))
  328.        * 10000);
  329.   register int pri2
  330.     = (((double) (floor_log2 (reg_n_refs[r2]) * reg_n_refs[r2])
  331.     / (reg_live_length[r2] * allocno_size[*v2]))
  332.        * 10000);
  333.   if (pri2 - pri1)
  334.     return pri2 - pri1;
  335.  
  336.   /* If regs are equally good, sort by allocno,
  337.      so that the results of qsort leave nothing to chance.  */
  338.   return *v1 - *v2;
  339. }
  340.  
  341. /* Scan the rtl code and record all conflicts in the conflict matrices.  */
  342.  
  343. static void
  344. global_conflicts ()
  345. {
  346.   register int b, i;
  347.   register rtx insn;
  348.   short *block_start_allocnos;
  349.  
  350.   /* Make a vector that mark_reg_{store,clobber} will store in.  */
  351.   regs_set = (rtx *) alloca (max_parallel * sizeof (rtx) * 2);
  352.  
  353.   block_start_allocnos = (short *) alloca (max_allocno * sizeof (short));
  354.  
  355.   for (b = 0; b < n_basic_blocks; b++)
  356.     {
  357.       bzero (allocnos_live, allocno_row_words * sizeof (int));
  358.  
  359.       /* Initialize table of registers currently live
  360.      to the state at the beginning of this basic block.
  361.      This also marks the conflicts among them.
  362.  
  363.      For pseudo-regs, there is only one bit for each one
  364.      no matter how many hard regs it occupies.
  365.      This is ok; we know the size from PSEUDO_REGNO_SIZE.
  366.      For explicit hard regs, we cannot know the size that way
  367.      since one hard reg can be used with various sizes.
  368.      Therefore, we must require that all the hard regs
  369.      implicitly live as part of a multi-word hard reg
  370.      are explicitly marked in basic_block_live_at_start.  */
  371.  
  372.       {
  373.     register int offset, bit;
  374.     register regset old = basic_block_live_at_start[b];
  375.     int ax = 0;
  376.  
  377. #ifdef HARD_REG_SET
  378.     hard_regs_live = old[0];
  379. #else
  380.     COPY_HARD_REG_SET (hard_regs_live, old);
  381. #endif
  382.     for (offset = 0, i = 0; offset < regset_size; offset++)
  383.       if (old[offset] == 0)
  384.         i += HOST_BITS_PER_INT;
  385.       else
  386.         for (bit = 1; bit; bit <<= 1, i++)
  387.           {
  388.         if (i >= max_regno)
  389.           break;
  390.         if (old[offset] & bit)
  391.           {
  392.             register int a = reg_allocno[i];
  393.             if (a >= 0)
  394.               {
  395.             SET_ALLOCNO_LIVE (a);
  396.             block_start_allocnos[ax++] = a;
  397.               }
  398.             else if ((a = reg_renumber[i]) >= 0)
  399.               mark_reg_live_nc (a, PSEUDO_REGNO_MODE (i));
  400.           }
  401.           }
  402.  
  403.     /* Record that each allocno now live conflicts with each other
  404.        allocno now live, and with each hard reg now live.  */
  405.  
  406.     record_conflicts (block_start_allocnos, ax);
  407.       }
  408.  
  409.       insn = basic_block_head[b];
  410.  
  411.       /* Scan the code of this basic block, noting which allocnos
  412.      and hard regs are born or die.  When one is born,
  413.      record a conflict with all others currently live.  */
  414.  
  415.       while (1)
  416.     {
  417.       register RTX_CODE code = GET_CODE (insn);
  418.       register rtx link;
  419.  
  420.       /* Make regs_set an empty set.  */
  421.  
  422.       n_regs_set = 0;
  423.  
  424.       if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
  425.         {
  426.           /* Mark any registers clobbered by INSN as live,
  427.          so they conflict with the inputs.  */
  428.  
  429.           note_stores (PATTERN (insn), mark_reg_clobber);
  430.  
  431.           /* Mark any registers dead after INSN as dead now.  */
  432.  
  433.           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  434.         if (REG_NOTE_KIND (link) == REG_DEAD)
  435.           mark_reg_death (XEXP (link, 0));
  436.  
  437.           /* Mark any registers set in INSN as live,
  438.          and mark them as conflicting with all other live regs.
  439.          Clobbers are processed again, so they conflict with
  440.          the registers that are set.  */
  441.  
  442.           note_stores (PATTERN (insn), mark_reg_store);
  443.  
  444.           /* Mark any registers both set and dead after INSN as dead.
  445.          This is not redundant!
  446.          A register may be set and killed in the same insn.
  447.          It is necessary to mark them as live, above, to get
  448.          the right conflicts within the insn.  */
  449.  
  450.           while (n_regs_set > 0)
  451.         if (find_regno_note (insn, REG_DEAD, REGNO (regs_set[--n_regs_set])))
  452.           mark_reg_death (regs_set[n_regs_set]);
  453.         
  454.           /* Likewise for regs set by incrementation.  */
  455.  
  456.           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  457.         if (REG_NOTE_KIND (link) == REG_INC
  458.             && find_regno_note (insn, REG_DEAD, REGNO (XEXP (link, 0))))
  459.           mark_reg_death (XEXP (link, 0));
  460.         }
  461.  
  462.       if (insn == basic_block_end[b])
  463.         break;
  464.       insn = NEXT_INSN (insn);
  465.     }
  466.     }
  467. }
  468.  
  469. /* Assign a hard register to ALLOCNO; look for one that is the beginning
  470.    of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
  471.    The registers marked in PREFREGS are tried first.
  472.  
  473.    If ALL_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
  474.    Otherwise ignore that preferred class.
  475.  
  476.    If ACCEPT_CALL_CLOBBERED is nonzero, accept a call-clobbered hard reg that
  477.    will have to be saved and restored at calls.
  478.  
  479.    If we find one, record it in reg_renumber.
  480.    If not, do nothing.  */
  481.  
  482. static void
  483. find_reg (allocno, losers, all_regs_p, accept_call_clobbered, prefregs)
  484.      int allocno;
  485.      register short *losers;
  486.      int all_regs_p;
  487.      int accept_call_clobbered;
  488.      HARD_REG_SET prefregs;
  489. {
  490.   register int i, prefreg, pass;
  491. #ifdef HARD_REG_SET
  492.   register        /* Declare it register if it's a scalar.  */
  493. #endif
  494.     HARD_REG_SET used;
  495.  
  496.   enum reg_class class 
  497.     = all_regs_p ? GENERAL_REGS : reg_preferred_class (allocno_reg[allocno]);
  498.   enum machine_mode mode = PSEUDO_REGNO_MODE (allocno_reg[allocno]);
  499.  
  500.   if (accept_call_clobbered)
  501.     COPY_HARD_REG_SET (used, call_fixed_reg_set);
  502.   else if (reg_n_calls_crossed[allocno_reg[allocno]] == 0)
  503.     COPY_HARD_REG_SET (used, fixed_reg_set);
  504.   else
  505.     COPY_HARD_REG_SET (used, call_used_reg_set);
  506.  
  507.   /* Some registers should not be allocated in global-alloc.  */
  508.   IOR_HARD_REG_SET (used, no_global_alloc_regs);
  509.  
  510.   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  511.   IOR_HARD_REG_SET (used, hard_reg_conflicts[allocno]);
  512.   if (frame_pointer_needed)
  513.     SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
  514.  
  515.   AND_COMPL_HARD_REG_SET (prefregs, used);
  516.  
  517.   /* Try to find a register from the preferred set first. */
  518.  
  519.   i = -1;
  520.   for (prefreg = 0; prefreg < FIRST_PSEUDO_REGISTER; prefreg++)
  521.     if (TEST_HARD_REG_BIT (prefregs, prefreg)
  522.     && (losers == 0 || losers[prefreg] < 0)
  523.     && HARD_REGNO_MODE_OK (prefreg, mode))
  524.       {
  525.     register int j;
  526.     register int lim = prefreg + HARD_REGNO_NREGS (prefreg, mode);
  527.     for (j = prefreg + 1;
  528.          (j < lim
  529.           && ! TEST_HARD_REG_BIT (used, j)
  530.           && (losers == 0 || losers[j] < 0));
  531.          j++);
  532.     if (j == lim)
  533.       {
  534.         i = prefreg;
  535.         break;
  536.       }
  537.       }
  538.  
  539. #if 0
  540.   /* Otherwise try each hard reg to see if it fits.  Do this in two passes.
  541.      In the first pass, skip registers that are prefered by some pseudo to
  542.      give it a better chance of getting one of those registers.  Only if
  543.      we can't get a register when excluding those do we take one of them.  */
  544.  
  545.   /* This is turned off because it makes worse allocation on the 68020.  */
  546.   for (pass = 0; pass <= 1 && i < 0; pass++)
  547. #endif
  548.   pass = 1;
  549.     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  550.       {
  551. #ifdef REG_ALLOC_ORDER
  552.     int regno = reg_alloc_order[i];
  553. #else
  554.     int regno = i;
  555. #endif
  556.     if (! TEST_HARD_REG_BIT (used, regno)
  557.         && (losers == 0 || losers[regno] < 0)
  558.         && (pass == 1 || ! TEST_HARD_REG_BIT (regs_someone_prefers, regno))
  559.         && HARD_REGNO_MODE_OK (regno, mode))
  560.       {
  561.         register int j;
  562.         register int lim = regno + HARD_REGNO_NREGS (regno, mode);
  563.         for (j = regno + 1;
  564.          (j < lim
  565.           && ! TEST_HARD_REG_BIT (used, j)
  566.           && (losers == 0 || losers[j] < 0));
  567.          j++);
  568.         if (j == lim)
  569.           {
  570.         i = regno;
  571.         break;
  572.           }
  573. #ifndef REG_ALLOC_ORDER
  574.         i = j;            /* Skip starting points we know will lose */
  575. #endif
  576.       }
  577.       }
  578.  
  579.   /* Did we find a register?  */
  580.  
  581.   if (i < FIRST_PSEUDO_REGISTER)
  582.     {
  583.       register int lim, j;
  584.       HARD_REG_SET this_reg;
  585.  
  586.       /* Yes.  Record it as the hard register of this pseudo-reg.  */
  587.       reg_renumber[allocno_reg[allocno]] = i;
  588.       /* For each other pseudo-reg conflicting with this one,
  589.      mark it as conflicting with the hard regs this one occupies.  */
  590.       CLEAR_HARD_REG_SET (this_reg);
  591.       lim = i + HARD_REGNO_NREGS (i, mode);
  592.       for (j = i; j < lim; j++)
  593.     SET_HARD_REG_BIT (this_reg, j);
  594.       lim = allocno;
  595.       for (j = 0; j < max_allocno; j++)
  596.     if (CONFLICTP (lim, j) || CONFLICTP (j, lim))
  597.       {
  598.         IOR_HARD_REG_SET (hard_reg_conflicts[j], this_reg);
  599.       }
  600.     }
  601.   else if (flag_caller_saves)
  602.     {
  603.       /* Did not find a register.  If it would be profitable to
  604.      allocate a call-clobbered register and save and restore it
  605.      around calls, do that.  */
  606.       if (! accept_call_clobbered
  607.       && reg_n_calls_crossed[allocno_reg[allocno]] != 0
  608.       && CALLER_SAVE_PROFITABLE (reg_n_refs[allocno_reg[allocno]],
  609.                      reg_n_calls_crossed[allocno_reg[allocno]]))
  610.     {
  611.       find_reg (allocno, losers, all_regs_p, 1, prefregs);
  612.       if (reg_renumber[allocno_reg[allocno]] >= 0)
  613.         caller_save_needed = 1;
  614.     }
  615.     }
  616. }
  617.  
  618. /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
  619.    Perhaps it had previously seemed not worth a hard reg,
  620.    or perhaps its old hard reg has been commandeered for reloads.
  621.    FORBIDDEN_REGS is a vector that indicates certain hard regs
  622.    that may not be used, even if they do not appear to be allocated.
  623.    A nonnegative element means the corresponding hard reg is forbidden.
  624.    If FORBIDDEN_REGS is zero, no regs are forbidden.  */
  625.  
  626. void
  627. retry_global_alloc (regno, forbidden_regs)
  628.      int regno;
  629.      short *forbidden_regs;
  630. {
  631.   int allocno = reg_allocno[regno];
  632.   if (allocno >= 0)
  633.     {
  634.       /* If we have more than one register class,
  635.      first try allocating in the class that is cheapest
  636.      for this pseudo-reg.  If that fails, try any reg.  */
  637.       if (N_REG_CLASSES > 1)
  638.     find_reg (allocno, forbidden_regs, 0, 0,
  639.           hard_reg_preferences[allocno]);
  640.       if (reg_renumber[regno] < 0
  641.       && !reg_preferred_or_nothing (regno))
  642.     find_reg (allocno, forbidden_regs, 1, 0,
  643.           hard_reg_preferences[allocno]);
  644.     }
  645. }
  646.  
  647. /* Called from reload pass to see if current function's pseudo regs
  648.    require a frame pointer to be allocated and set up.
  649.  
  650.    Return 1 if so, 0 otherwise.
  651.    We may alter the hard-reg allocation of the pseudo regs
  652.    in order to make the frame pointer unnecessary.
  653.    However, if the value is 1, nothing has been altered.
  654.  
  655.    Args grant access to some tables used in reload1.c.
  656.    See there for info on them.  */
  657.  
  658. int
  659. check_frame_pointer_required (reg_equiv_constant, reg_equiv_mem, reg_equiv_address)
  660.      rtx *reg_equiv_constant, *reg_equiv_mem, *reg_equiv_address;
  661. {
  662.   register int i;
  663.   HARD_REG_SET *old_hard_reg_conflicts;
  664.   short *old_reg_renumber;
  665.   char old_regs_ever_live[FIRST_PSEUDO_REGISTER];
  666.  
  667.   /* If any pseudo reg has no hard reg and no equivalent,
  668.      we must have a frame pointer.  */
  669.  
  670.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  671.     if (reg_renumber[i] < 0 && reg_n_refs[i] > 0
  672.         && reg_equiv_mem[i] == 0 && reg_equiv_constant[i] == 0
  673.     && reg_equiv_address[i] == 0)
  674.       return 1;
  675.  
  676.   /* If we might not need a frame pointer,
  677.      try finding a hard reg for any pseudo that has a memory equivalent.
  678.      That is because the memory equivalent probably refers to a frame
  679.      pointer.  */
  680.  
  681.   old_reg_renumber = (short *) alloca (max_regno * sizeof (short));
  682.   old_hard_reg_conflicts = (HARD_REG_SET *)
  683.     alloca (max_allocno * sizeof (HARD_REG_SET));
  684.  
  685.   bcopy (reg_renumber, old_reg_renumber, max_regno * sizeof (short));
  686.   bcopy (hard_reg_conflicts, old_hard_reg_conflicts,
  687.      max_allocno * sizeof (HARD_REG_SET));
  688.   bcopy (regs_ever_live, old_regs_ever_live, sizeof regs_ever_live);
  689.  
  690.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  691.     if (reg_renumber[i] < 0
  692.     && ((reg_equiv_mem[i]
  693.          && reg_mentioned_p (frame_pointer_rtx, reg_equiv_mem[i]))
  694.         || (reg_equiv_address[i]
  695.         && reg_mentioned_p (frame_pointer_rtx, reg_equiv_address[i]))))
  696.       {
  697.     retry_global_alloc (i, 0);
  698.     /* If we can't find a hard reg for ALL of them,
  699.        or if a previously unneeded hard reg is used that requires saving,
  700.        we fail: set all those pseudos back as they were.  */
  701.     if (reg_renumber[i] < 0
  702.         || (! old_regs_ever_live[reg_renumber[i]]
  703.         && ! call_used_regs[reg_renumber[i]]))
  704.       {
  705.         bcopy (old_reg_renumber, reg_renumber,
  706.            max_regno * sizeof (short));
  707.         bcopy (old_hard_reg_conflicts, hard_reg_conflicts,
  708.            max_allocno * sizeof (HARD_REG_SET));
  709.         bcopy (old_regs_ever_live, regs_ever_live, sizeof regs_ever_live);
  710.         return 1;
  711.       }
  712.     mark_home_live (i);
  713.       }
  714.  
  715.   return 0;
  716. }
  717.  
  718. /* Record a conflict between register REGNO
  719.    and everything currently live.
  720.    REGNO must not be a pseudo reg that was allocated
  721.    by local_alloc; such numbers must be translated through
  722.    reg_renumber before calling here.  */
  723.  
  724. static void
  725. record_one_conflict (regno)
  726.      int regno;
  727. {
  728.   register int j;
  729.  
  730.   if (regno < FIRST_PSEUDO_REGISTER)
  731.     /* When a hard register becomes live,
  732.        record conflicts with live pseudo regs.  */
  733.     for (j = 0; j < max_allocno; j++)
  734.       {
  735.     if (ALLOCNO_LIVE_P (j))
  736.       SET_HARD_REG_BIT (hard_reg_conflicts[j], regno);
  737.       }
  738.   else
  739.     /* When a pseudo-register becomes live,
  740.        record conflicts first with hard regs,
  741.        then with other pseudo regs.  */
  742.     {
  743.       register int ialloc = reg_allocno[regno];
  744.       register int ialloc_prod = ialloc * allocno_row_words;
  745.       IOR_HARD_REG_SET (hard_reg_conflicts[ialloc], hard_regs_live);
  746.       for (j = allocno_row_words - 1; j >= 0; j--)
  747.     conflicts[ialloc_prod + j] |= allocnos_live[j];
  748.     }
  749. }
  750.  
  751. /* Record all allocnos currently live as conflicting
  752.    with each other and with all hard regs currently live.
  753.    ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
  754.    are currently live.  Their bits are also flagged in allocnos_live.  */
  755.  
  756. static void
  757. record_conflicts (allocno_vec, len)
  758.      register short *allocno_vec;
  759.      register int len;
  760. {
  761.   register int allocno;
  762.   register int j;
  763.   register int ialloc_prod;
  764.  
  765.   while (--len >= 0)
  766.     {
  767.       allocno = allocno_vec[len];
  768.       ialloc_prod = allocno * allocno_row_words;
  769.       IOR_HARD_REG_SET (hard_reg_conflicts[allocno], hard_regs_live);
  770.       for (j = allocno_row_words - 1; j >= 0; j--)
  771.     conflicts[ialloc_prod + j] |= allocnos_live[j];
  772.     }
  773. }
  774.  
  775. /* Handle the case where REG is set by the insn being scanned,
  776.    during the forward scan to accumulate conflicts.
  777.    Store a 1 in regs_live or allocnos_live for this register, record how many
  778.    consecutive hardware registers it actually needs,
  779.    and record a conflict with all other registers already live.
  780.  
  781.    Note that even if REG does not remain alive after this insn,
  782.    we must mark it here as live, to ensure a conflict between
  783.    REG and any other regs set in this insn that really do live.
  784.    This is because those other regs could be considered after this.
  785.  
  786.    REG might actually be something other than a register;
  787.    if so, we do nothing.
  788.  
  789.    CLOBBERs are processed here by calling mark_reg_clobber.  */ 
  790.  
  791. static void
  792. mark_reg_store (orig_reg, setter)
  793.      rtx orig_reg, setter;
  794. {
  795.   register int regno;
  796.   register rtx reg = orig_reg;
  797.  
  798.   /* WORD is which word of a multi-register group is being stored.
  799.      For the case where the store is actually into a SUBREG of REG.
  800.      Except we don't use it; I believe the entire REG needs to be
  801.      made live.  */
  802.   int word = 0;
  803.  
  804.   if (GET_CODE (reg) == SUBREG)
  805.     {
  806.       word = SUBREG_WORD (reg);
  807.       reg = SUBREG_REG (reg);
  808.     }
  809.  
  810.   if (GET_CODE (reg) != REG)
  811.     return;
  812.  
  813.   if (GET_CODE (setter) != SET)
  814.     {
  815.       /* A clobber of a register should be processed here too.  */
  816.       mark_reg_clobber (orig_reg, setter);
  817.       return;
  818.     }
  819.  
  820.   regs_set[n_regs_set++] = reg;
  821.  
  822.   set_preference (reg, SET_SRC (setter));
  823.  
  824.   regno = REGNO (reg);
  825.  
  826.   if (reg_renumber[regno] >= 0)
  827.     regno = reg_renumber[regno] /* + word */;
  828.  
  829.   /* Either this is one of the max_allocno pseudo regs not allocated,
  830.      or it is or has a hardware reg.  First handle the pseudo-regs.  */
  831.   if (regno >= FIRST_PSEUDO_REGISTER)
  832.     {
  833.       if (reg_allocno[regno] >= 0)
  834.     {
  835.       SET_ALLOCNO_LIVE (reg_allocno[regno]);
  836.       record_one_conflict (regno);
  837.     }
  838.     }
  839.   /* Handle hardware regs (and pseudos allocated to hard regs).  */
  840.   else if (! fixed_regs[regno])
  841.     {
  842.       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  843.       while (regno < last)
  844.     {
  845.       record_one_conflict (regno);
  846.       SET_HARD_REG_BIT (hard_regs_live, regno);
  847.       regno++;
  848.     }
  849.     }
  850. }
  851.  
  852. /* Like mark_reg_set except notice just CLOBBERs; ignore SETs.  */
  853.  
  854. static void
  855. mark_reg_clobber (reg, setter)
  856.      rtx reg, setter;
  857. {
  858.   register int regno;
  859.  
  860.   /* WORD is which word of a multi-register group is being stored.
  861.      For the case where the store is actually into a SUBREG of REG.
  862.      Except we don't use it; I believe the entire REG needs to be
  863.      made live.  */
  864.   int word = 0;
  865.  
  866.   if (GET_CODE (setter) != CLOBBER)
  867.     return;
  868.  
  869.   if (GET_CODE (reg) == SUBREG)
  870.     {
  871.       word = SUBREG_WORD (reg);
  872.       reg = SUBREG_REG (reg);
  873.     }
  874.  
  875.   if (GET_CODE (reg) != REG)
  876.     return;
  877.  
  878.   regs_set[n_regs_set++] = reg;
  879.  
  880.   regno = REGNO (reg);
  881.  
  882.   if (reg_renumber[regno] >= 0)
  883.     regno = reg_renumber[regno] /* + word */;
  884.  
  885.   /* Either this is one of the max_allocno pseudo regs not allocated,
  886.      or it is or has a hardware reg.  First handle the pseudo-regs.  */
  887.   if (regno >= FIRST_PSEUDO_REGISTER)
  888.     {
  889.       if (reg_allocno[regno] >= 0)
  890.     {
  891.       SET_ALLOCNO_LIVE (reg_allocno[regno]);
  892.       record_one_conflict (regno);
  893.     }
  894.     }
  895.   /* Handle hardware regs (and pseudos allocated to hard regs).  */
  896.   else if (! fixed_regs[regno])
  897.     {
  898.       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  899.       while (regno < last)
  900.     {
  901.       record_one_conflict (regno);
  902.       SET_HARD_REG_BIT (hard_regs_live, regno);
  903.       regno++;
  904.     }
  905.     }
  906. }
  907.  
  908. /* Mark REG as being dead (following the insn being scanned now).
  909.    Store a 0 in regs_live or allocnos_live for this register.  */
  910.  
  911. static void
  912. mark_reg_death (reg)
  913.      rtx reg;
  914. {
  915.   register int regno = REGNO (reg);
  916.  
  917.   /* For pseudo reg, see if it has been assigned a hardware reg.  */
  918.   if (reg_renumber[regno] >= 0)
  919.     regno = reg_renumber[regno];
  920.  
  921.   /* Either this is one of the max_allocno pseudo regs not allocated,
  922.      or it is a hardware reg.  First handle the pseudo-regs.  */
  923.   if (regno >= FIRST_PSEUDO_REGISTER)
  924.     {
  925.       if (reg_allocno[regno] >= 0)
  926.     CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
  927.     }
  928.   /* Handle hardware regs (and pseudos allocated to hard regs).  */
  929.   else if (! fixed_regs[regno])
  930.     {
  931.       /* Pseudo regs already assigned hardware regs are treated
  932.      almost the same as explicit hardware regs.  */
  933.       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  934.       while (regno < last)
  935.     {
  936.       CLEAR_HARD_REG_BIT (hard_regs_live, regno);
  937.       regno++;
  938.     }
  939.     }
  940. }
  941.  
  942. /* Mark hard reg REGNO as currently live, assuming machine mode MODE
  943.    for the value stored in it.  MODE determines how many consecutive
  944.    registers are actually in use.  Do not record conflicts;
  945.    it is assumed that the caller will do that.  */
  946.  
  947. static void
  948. mark_reg_live_nc (regno, mode)
  949.      register int regno;
  950.      enum machine_mode mode;
  951. {
  952.   register int last = regno + HARD_REGNO_NREGS (regno, mode);
  953.   while (regno < last)
  954.     {
  955.       SET_HARD_REG_BIT (hard_regs_live, regno);
  956.       regno++;
  957.     }
  958. }
  959.  
  960. /* Try to set a preference for an allocno to a hard register.
  961.    We are passed DEST and SRC which are the operands of a SET.  It is known
  962.    that SRC is a register.  If SRC or the first operand of SRC is a register,
  963.    try to set a preference.  If one of the two is a hard register and the other
  964.    is a pseudo-register, mark the preference.
  965.    
  966.    Note that we are not as agressive as local-alloc in trying to tie a
  967.    pseudo-register to a hard register.  */
  968.  
  969. static void
  970. set_preference (dest, src)
  971.      rtx dest, src;
  972. {
  973.   int src_regno, dest_regno;
  974.   /* Amount to add to the hard regno for SRC, or subtract from that for DEST,
  975.      to compensate for subregs in SRC or DEST.  */
  976.   int offset = 0;
  977.  
  978.   if (GET_RTX_FORMAT (GET_CODE (src))[0] == 'e')
  979.     src = XEXP (src, 0);
  980.  
  981.   /* Get the reg number for both SRC and DEST.
  982.      If neither is a reg, give up.  */
  983.  
  984.   if (GET_CODE (src) == REG)
  985.     src_regno = REGNO (src);
  986.   else if (GET_CODE (src) == SUBREG && GET_CODE (SUBREG_REG (src)) == REG)
  987.     {
  988.       src_regno = REGNO (SUBREG_REG (src));
  989.       offset += SUBREG_WORD (src);
  990.     }
  991.   else
  992.     return;
  993.  
  994.   if (GET_CODE (dest) == REG)
  995.     dest_regno = REGNO (dest);
  996.   else if (GET_CODE (dest) == SUBREG && GET_CODE (SUBREG_REG (dest)) == REG)
  997.     {
  998.       dest_regno = REGNO (SUBREG_REG (dest));
  999.       offset -= SUBREG_WORD (dest);
  1000.     }
  1001.   else
  1002.     return;
  1003.  
  1004.   /* Convert either or both to hard reg numbers.  */
  1005.  
  1006.   if (reg_renumber[src_regno] >= 0)
  1007.     src_regno = reg_renumber[src_regno];
  1008.  
  1009.   if (reg_renumber[dest_regno] >= 0)
  1010.     dest_regno = reg_renumber[dest_regno];
  1011.  
  1012.   /* Now if one is a hard reg and the other is a global pseudo
  1013.      then give the other a preference.  */
  1014.  
  1015.   if (dest_regno < FIRST_PSEUDO_REGISTER && src_regno >= FIRST_PSEUDO_REGISTER
  1016.       && reg_allocno[src_regno] >= 0)
  1017.     {
  1018.       dest_regno -= offset;
  1019.       if (dest_regno >= 0 && dest_regno < FIRST_PSEUDO_REGISTER)
  1020.     {
  1021.       SET_REGBIT (hard_reg_preferences,
  1022.               reg_allocno[src_regno], dest_regno);
  1023.       SET_HARD_REG_BIT (regs_someone_prefers, dest_regno);
  1024.     }
  1025.     }
  1026.  
  1027.   if (src_regno < FIRST_PSEUDO_REGISTER && dest_regno >= FIRST_PSEUDO_REGISTER
  1028.       && reg_allocno[dest_regno] >= 0)
  1029.     {
  1030.       src_regno += offset;
  1031.       if (src_regno >= 0 && src_regno < FIRST_PSEUDO_REGISTER)
  1032.     {
  1033.       SET_REGBIT (hard_reg_preferences,
  1034.               reg_allocno[dest_regno], src_regno);
  1035.       SET_HARD_REG_BIT (regs_someone_prefers, src_regno);
  1036.     }
  1037.     }
  1038. }
  1039.  
  1040. /* Print debugging trace information if -greg switch is given,
  1041.    showing the information on which the allocation decisions are based.  */
  1042.  
  1043. static void
  1044. dump_conflicts (file)
  1045.      FILE *file;
  1046. {
  1047.   register int i;
  1048.   fprintf (file, ";; %d regs to allocate:", max_allocno);
  1049.   for (i = 0; i < max_allocno; i++)
  1050.     {
  1051.       fprintf (file, " %d", allocno_reg[allocno_order[i]]);
  1052.       if (allocno_size[allocno_order[i]] != 1)
  1053.     fprintf (file, " (%d)", allocno_size[allocno_order[i]]);
  1054.     }
  1055.   fprintf (file, "\n");
  1056.  
  1057.   for (i = 0; i < max_allocno; i++)
  1058.     {
  1059.       register int j;
  1060.       fprintf (file, ";; %d conflicts:", allocno_reg[i]);
  1061.       for (j = 0; j < max_allocno; j++)
  1062.     if (CONFLICTP (i, j) || CONFLICTP (j, i))
  1063.       fprintf (file, " %d", allocno_reg[j]);
  1064.       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
  1065.     if (TEST_HARD_REG_BIT (hard_reg_conflicts[i], j))
  1066.       fprintf (file, " %d", j);
  1067.       fprintf (file, "\n");
  1068.     }
  1069.   fprintf (file, "\n");
  1070. }
  1071.  
  1072. void
  1073. dump_global_regs (file)
  1074.      FILE *file;
  1075. {
  1076.   register int i;
  1077.  
  1078.   fprintf (file, ";; Register dispositions:");
  1079.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1080.     {
  1081.       if (reg_renumber[i] >= 0)
  1082.     fprintf (file, " %d in %d ", i, reg_renumber[i]);
  1083.     }
  1084.  
  1085.   fprintf (file, "\n\n;; Hard regs used: ");
  1086.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1087.     if (regs_ever_live[i])
  1088.       fprintf (file, " %d", i);
  1089.   fprintf (file, "\n\n");
  1090. }
  1091.